/*
 *   Copyright © 2011-2015 Kinghood Group All Rights Reserved.
 *   未经本公司正式书面同意，其他任何个人、团体不得使用、复制、修改或发布本软件.
 *   版权所有深圳金雅福控股集团有限公司 www.jinyafu.com.
 */

package com.kinghood.authoritycenter.common.aspect;

import com.alibaba.fastjson.JSON;
import com.kinghood.authoritycenter.common.util.UserUtil;
import com.kinghood.authoritycenter.service.IAsyncSysLogService;
import com.kinghood.infrastructure.api.authority.AddSysLogReqDTO;
import com.kinghood.infrastructure.aspect.OperatorLog;
import com.kinghood.infrastructure.model.KingHoodResponseVO;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * 系统日志
 *
 * @author zfj
 * @date 2023/12/15
 */
@Aspect
@Component
@Slf4j
public class OperatorLogAop {

    @Resource
    private IAsyncSysLogService asyncSysLogService;


    @Pointcut(value = "@annotation(com.kinghood.infrastructure.aspect.OperatorLog)")
    private void joinPointCut() {

    }

    @Around(value = "joinPointCut() && @annotation(operatorLog)")
    public Object MetricsCollector(ProceedingJoinPoint joinPoint, OperatorLog operatorLog) throws Throwable {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        if (Objects.isNull(requestAttributes)) {
            return joinPoint.proceed();
        }
        HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
        String url = request.getRequestURI();
        log.info("请求的url：{}", url);

        if (operatorLog != null) {
            AddSysLogReqDTO add = new AddSysLogReqDTO();
            add.setUrl(url);
            add.setUserId(UserUtil.getUserId());
            add.setModule(operatorLog.module());
            add.setContent(operatorLog.content());
            add.setRequestContent(JSON.toJSONString(joinPoint.getArgs()));

            Object object = null;
            try {
                object = joinPoint.proceed();

                if (Objects.nonNull(object)) {
                    add.setResponseContent(JSON.toJSONString(object));
                }
                if (object instanceof KingHoodResponseVO) {
                    KingHoodResponseVO result = (KingHoodResponseVO) object;
                    log.info("OperatorLogAspect==>返回：{}", result);
                } else {
                    log.info("OperatorLogAspect==>返回不是KingHoodResponseVO");
                }
            } catch (Exception e) {
                add.setIsException(1);
                add.setErrorMessage(e.getMessage());
                log.error("OperatorLogAspect==>返回异常：", e);
                throw e;
            } finally {
                if (Objects.nonNull(object)) {
                    add.setResponseContent(JSON.toJSONString(object));
                }
                asyncSysLogService.addSysLog(add);

            }
            return object;
        } else {
            return joinPoint.proceed();
        }
    }

}